{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 4.5 TensorBordXによるネットワークモデルの可視化\n",
    "\n",
    "- 本ファイルでは、OpenPoseのネットワークモデルをTensorBordで可視化します\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 4.5 学習目標\n",
    "\n",
    "1.\ttensorbordXが動作する環境を構築できるようになる\n",
    "2.\tOpenPoseNetクラスを対象に、tensorbordXでネットワーク（graph）を可視化するファイルを出力できるようになる\n",
    "3.\ttensorbordXのgraphファイルをブラウザで描画し、テンソルサイズの確認などができるようになる\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 事前準備\n",
    "\n",
    "tensorbordXを利用するためにtensorbordXとTensorFlowをインストールする必要があります。以下のようにインストールしてください。\n",
    "\n",
    "pip install tensorflow \n",
    "\n",
    "pip install tensorboardx\n",
    "\n",
    "※　pip install tensorflow tensorboardx　でやるとうまくインストールできないようです。。\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 必要なパッケージのimport\n",
    "import torch"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "OpenPoseNet(\n",
       "  (model0): OpenPose_Feature(\n",
       "    (model): Sequential(\n",
       "      (0): Conv2d(3, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))\n",
       "      (1): ReLU(inplace)\n",
       "      (2): Conv2d(64, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))\n",
       "      (3): ReLU(inplace)\n",
       "      (4): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)\n",
       "      (5): Conv2d(64, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))\n",
       "      (6): ReLU(inplace)\n",
       "      (7): Conv2d(128, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))\n",
       "      (8): ReLU(inplace)\n",
       "      (9): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)\n",
       "      (10): Conv2d(128, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))\n",
       "      (11): ReLU(inplace)\n",
       "      (12): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))\n",
       "      (13): ReLU(inplace)\n",
       "      (14): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))\n",
       "      (15): ReLU(inplace)\n",
       "      (16): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))\n",
       "      (17): ReLU(inplace)\n",
       "      (18): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)\n",
       "      (19): Conv2d(256, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))\n",
       "      (20): ReLU(inplace)\n",
       "      (21): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))\n",
       "      (22): ReLU(inplace)\n",
       "      (23): Conv2d(512, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))\n",
       "      (24): ReLU(inplace)\n",
       "      (25): Conv2d(256, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))\n",
       "      (26): ReLU(inplace)\n",
       "    )\n",
       "  )\n",
       "  (model1_1): Sequential(\n",
       "    (0): Conv2d(128, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))\n",
       "    (1): ReLU(inplace)\n",
       "    (2): Conv2d(128, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))\n",
       "    (3): ReLU(inplace)\n",
       "    (4): Conv2d(128, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))\n",
       "    (5): ReLU(inplace)\n",
       "    (6): Conv2d(128, 512, kernel_size=(1, 1), stride=(1, 1))\n",
       "    (7): ReLU(inplace)\n",
       "    (8): Conv2d(512, 38, kernel_size=(1, 1), stride=(1, 1))\n",
       "  )\n",
       "  (model2_1): Sequential(\n",
       "    (0): Conv2d(185, 128, kernel_size=(7, 7), stride=(1, 1), padding=(3, 3))\n",
       "    (1): ReLU(inplace)\n",
       "    (2): Conv2d(128, 128, kernel_size=(7, 7), stride=(1, 1), padding=(3, 3))\n",
       "    (3): ReLU(inplace)\n",
       "    (4): Conv2d(128, 128, kernel_size=(7, 7), stride=(1, 1), padding=(3, 3))\n",
       "    (5): ReLU(inplace)\n",
       "    (6): Conv2d(128, 128, kernel_size=(7, 7), stride=(1, 1), padding=(3, 3))\n",
       "    (7): ReLU(inplace)\n",
       "    (8): Conv2d(128, 128, kernel_size=(7, 7), stride=(1, 1), padding=(3, 3))\n",
       "    (9): ReLU(inplace)\n",
       "    (10): Conv2d(128, 128, kernel_size=(1, 1), stride=(1, 1))\n",
       "    (11): ReLU(inplace)\n",
       "    (12): Conv2d(128, 38, kernel_size=(1, 1), stride=(1, 1))\n",
       "  )\n",
       "  (model3_1): Sequential(\n",
       "    (0): Conv2d(185, 128, kernel_size=(7, 7), stride=(1, 1), padding=(3, 3))\n",
       "    (1): ReLU(inplace)\n",
       "    (2): Conv2d(128, 128, kernel_size=(7, 7), stride=(1, 1), padding=(3, 3))\n",
       "    (3): ReLU(inplace)\n",
       "    (4): Conv2d(128, 128, kernel_size=(7, 7), stride=(1, 1), padding=(3, 3))\n",
       "    (5): ReLU(inplace)\n",
       "    (6): Conv2d(128, 128, kernel_size=(7, 7), stride=(1, 1), padding=(3, 3))\n",
       "    (7): ReLU(inplace)\n",
       "    (8): Conv2d(128, 128, kernel_size=(7, 7), stride=(1, 1), padding=(3, 3))\n",
       "    (9): ReLU(inplace)\n",
       "    (10): Conv2d(128, 128, kernel_size=(1, 1), stride=(1, 1))\n",
       "    (11): ReLU(inplace)\n",
       "    (12): Conv2d(128, 38, kernel_size=(1, 1), stride=(1, 1))\n",
       "  )\n",
       "  (model4_1): Sequential(\n",
       "    (0): Conv2d(185, 128, kernel_size=(7, 7), stride=(1, 1), padding=(3, 3))\n",
       "    (1): ReLU(inplace)\n",
       "    (2): Conv2d(128, 128, kernel_size=(7, 7), stride=(1, 1), padding=(3, 3))\n",
       "    (3): ReLU(inplace)\n",
       "    (4): Conv2d(128, 128, kernel_size=(7, 7), stride=(1, 1), padding=(3, 3))\n",
       "    (5): ReLU(inplace)\n",
       "    (6): Conv2d(128, 128, kernel_size=(7, 7), stride=(1, 1), padding=(3, 3))\n",
       "    (7): ReLU(inplace)\n",
       "    (8): Conv2d(128, 128, kernel_size=(7, 7), stride=(1, 1), padding=(3, 3))\n",
       "    (9): ReLU(inplace)\n",
       "    (10): Conv2d(128, 128, kernel_size=(1, 1), stride=(1, 1))\n",
       "    (11): ReLU(inplace)\n",
       "    (12): Conv2d(128, 38, kernel_size=(1, 1), stride=(1, 1))\n",
       "  )\n",
       "  (model5_1): Sequential(\n",
       "    (0): Conv2d(185, 128, kernel_size=(7, 7), stride=(1, 1), padding=(3, 3))\n",
       "    (1): ReLU(inplace)\n",
       "    (2): Conv2d(128, 128, kernel_size=(7, 7), stride=(1, 1), padding=(3, 3))\n",
       "    (3): ReLU(inplace)\n",
       "    (4): Conv2d(128, 128, kernel_size=(7, 7), stride=(1, 1), padding=(3, 3))\n",
       "    (5): ReLU(inplace)\n",
       "    (6): Conv2d(128, 128, kernel_size=(7, 7), stride=(1, 1), padding=(3, 3))\n",
       "    (7): ReLU(inplace)\n",
       "    (8): Conv2d(128, 128, kernel_size=(7, 7), stride=(1, 1), padding=(3, 3))\n",
       "    (9): ReLU(inplace)\n",
       "    (10): Conv2d(128, 128, kernel_size=(1, 1), stride=(1, 1))\n",
       "    (11): ReLU(inplace)\n",
       "    (12): Conv2d(128, 38, kernel_size=(1, 1), stride=(1, 1))\n",
       "  )\n",
       "  (model6_1): Sequential(\n",
       "    (0): Conv2d(185, 128, kernel_size=(7, 7), stride=(1, 1), padding=(3, 3))\n",
       "    (1): ReLU(inplace)\n",
       "    (2): Conv2d(128, 128, kernel_size=(7, 7), stride=(1, 1), padding=(3, 3))\n",
       "    (3): ReLU(inplace)\n",
       "    (4): Conv2d(128, 128, kernel_size=(7, 7), stride=(1, 1), padding=(3, 3))\n",
       "    (5): ReLU(inplace)\n",
       "    (6): Conv2d(128, 128, kernel_size=(7, 7), stride=(1, 1), padding=(3, 3))\n",
       "    (7): ReLU(inplace)\n",
       "    (8): Conv2d(128, 128, kernel_size=(7, 7), stride=(1, 1), padding=(3, 3))\n",
       "    (9): ReLU(inplace)\n",
       "    (10): Conv2d(128, 128, kernel_size=(1, 1), stride=(1, 1))\n",
       "    (11): ReLU(inplace)\n",
       "    (12): Conv2d(128, 38, kernel_size=(1, 1), stride=(1, 1))\n",
       "  )\n",
       "  (model1_2): Sequential(\n",
       "    (0): Conv2d(128, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))\n",
       "    (1): ReLU(inplace)\n",
       "    (2): Conv2d(128, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))\n",
       "    (3): ReLU(inplace)\n",
       "    (4): Conv2d(128, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))\n",
       "    (5): ReLU(inplace)\n",
       "    (6): Conv2d(128, 512, kernel_size=(1, 1), stride=(1, 1))\n",
       "    (7): ReLU(inplace)\n",
       "    (8): Conv2d(512, 19, kernel_size=(1, 1), stride=(1, 1))\n",
       "  )\n",
       "  (model2_2): Sequential(\n",
       "    (0): Conv2d(185, 128, kernel_size=(7, 7), stride=(1, 1), padding=(3, 3))\n",
       "    (1): ReLU(inplace)\n",
       "    (2): Conv2d(128, 128, kernel_size=(7, 7), stride=(1, 1), padding=(3, 3))\n",
       "    (3): ReLU(inplace)\n",
       "    (4): Conv2d(128, 128, kernel_size=(7, 7), stride=(1, 1), padding=(3, 3))\n",
       "    (5): ReLU(inplace)\n",
       "    (6): Conv2d(128, 128, kernel_size=(7, 7), stride=(1, 1), padding=(3, 3))\n",
       "    (7): ReLU(inplace)\n",
       "    (8): Conv2d(128, 128, kernel_size=(7, 7), stride=(1, 1), padding=(3, 3))\n",
       "    (9): ReLU(inplace)\n",
       "    (10): Conv2d(128, 128, kernel_size=(1, 1), stride=(1, 1))\n",
       "    (11): ReLU(inplace)\n",
       "    (12): Conv2d(128, 19, kernel_size=(1, 1), stride=(1, 1))\n",
       "  )\n",
       "  (model3_2): Sequential(\n",
       "    (0): Conv2d(185, 128, kernel_size=(7, 7), stride=(1, 1), padding=(3, 3))\n",
       "    (1): ReLU(inplace)\n",
       "    (2): Conv2d(128, 128, kernel_size=(7, 7), stride=(1, 1), padding=(3, 3))\n",
       "    (3): ReLU(inplace)\n",
       "    (4): Conv2d(128, 128, kernel_size=(7, 7), stride=(1, 1), padding=(3, 3))\n",
       "    (5): ReLU(inplace)\n",
       "    (6): Conv2d(128, 128, kernel_size=(7, 7), stride=(1, 1), padding=(3, 3))\n",
       "    (7): ReLU(inplace)\n",
       "    (8): Conv2d(128, 128, kernel_size=(7, 7), stride=(1, 1), padding=(3, 3))\n",
       "    (9): ReLU(inplace)\n",
       "    (10): Conv2d(128, 128, kernel_size=(1, 1), stride=(1, 1))\n",
       "    (11): ReLU(inplace)\n",
       "    (12): Conv2d(128, 19, kernel_size=(1, 1), stride=(1, 1))\n",
       "  )\n",
       "  (model4_2): Sequential(\n",
       "    (0): Conv2d(185, 128, kernel_size=(7, 7), stride=(1, 1), padding=(3, 3))\n",
       "    (1): ReLU(inplace)\n",
       "    (2): Conv2d(128, 128, kernel_size=(7, 7), stride=(1, 1), padding=(3, 3))\n",
       "    (3): ReLU(inplace)\n",
       "    (4): Conv2d(128, 128, kernel_size=(7, 7), stride=(1, 1), padding=(3, 3))\n",
       "    (5): ReLU(inplace)\n",
       "    (6): Conv2d(128, 128, kernel_size=(7, 7), stride=(1, 1), padding=(3, 3))\n",
       "    (7): ReLU(inplace)\n",
       "    (8): Conv2d(128, 128, kernel_size=(7, 7), stride=(1, 1), padding=(3, 3))\n",
       "    (9): ReLU(inplace)\n",
       "    (10): Conv2d(128, 128, kernel_size=(1, 1), stride=(1, 1))\n",
       "    (11): ReLU(inplace)\n",
       "    (12): Conv2d(128, 19, kernel_size=(1, 1), stride=(1, 1))\n",
       "  )\n",
       "  (model5_2): Sequential(\n",
       "    (0): Conv2d(185, 128, kernel_size=(7, 7), stride=(1, 1), padding=(3, 3))\n",
       "    (1): ReLU(inplace)\n",
       "    (2): Conv2d(128, 128, kernel_size=(7, 7), stride=(1, 1), padding=(3, 3))\n",
       "    (3): ReLU(inplace)\n",
       "    (4): Conv2d(128, 128, kernel_size=(7, 7), stride=(1, 1), padding=(3, 3))\n",
       "    (5): ReLU(inplace)\n",
       "    (6): Conv2d(128, 128, kernel_size=(7, 7), stride=(1, 1), padding=(3, 3))\n",
       "    (7): ReLU(inplace)\n",
       "    (8): Conv2d(128, 128, kernel_size=(7, 7), stride=(1, 1), padding=(3, 3))\n",
       "    (9): ReLU(inplace)\n",
       "    (10): Conv2d(128, 128, kernel_size=(1, 1), stride=(1, 1))\n",
       "    (11): ReLU(inplace)\n",
       "    (12): Conv2d(128, 19, kernel_size=(1, 1), stride=(1, 1))\n",
       "  )\n",
       "  (model6_2): Sequential(\n",
       "    (0): Conv2d(185, 128, kernel_size=(7, 7), stride=(1, 1), padding=(3, 3))\n",
       "    (1): ReLU(inplace)\n",
       "    (2): Conv2d(128, 128, kernel_size=(7, 7), stride=(1, 1), padding=(3, 3))\n",
       "    (3): ReLU(inplace)\n",
       "    (4): Conv2d(128, 128, kernel_size=(7, 7), stride=(1, 1), padding=(3, 3))\n",
       "    (5): ReLU(inplace)\n",
       "    (6): Conv2d(128, 128, kernel_size=(7, 7), stride=(1, 1), padding=(3, 3))\n",
       "    (7): ReLU(inplace)\n",
       "    (8): Conv2d(128, 128, kernel_size=(7, 7), stride=(1, 1), padding=(3, 3))\n",
       "    (9): ReLU(inplace)\n",
       "    (10): Conv2d(128, 128, kernel_size=(1, 1), stride=(1, 1))\n",
       "    (11): ReLU(inplace)\n",
       "    (12): Conv2d(128, 19, kernel_size=(1, 1), stride=(1, 1))\n",
       "  )\n",
       ")"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from utils.openpose_net import OpenPoseNet\n",
    "# モデルの用意\n",
    "net = OpenPoseNet()\n",
    "net.train()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 1. tensorboardXの保存クラスを呼び出します\n",
    "from tensorboardX import SummaryWriter\n",
    "\n",
    "# 2. フォルダ「tbX」に保存させるwriterを用意します\n",
    "# フォルダ「tbX」はなければ自動で作成されます\n",
    "writer = SummaryWriter(\"./tbX/\")\n",
    "\n",
    "\n",
    "# 3. ネットワークに流し込むダミーデータを作成します\n",
    "batch_size = 2\n",
    "dummy_img = torch.rand(batch_size, 3, 368, 368)\n",
    "\n",
    "# 4. OpenPoseのインスタンスnetに対して、ダミーデータである\n",
    "# dummy_imgを流したときのgraphをwriterに保存させます\n",
    "writer.add_graph(net, (dummy_img, ))\n",
    "writer.close()\n",
    "\n",
    "\n",
    "# 5. コマンドプロンプトを開き、フォルダ「tbX」がある\n",
    "# フォルダ「4_pose_estimation」まで移動して、\n",
    "# 以下のコマンドを実行します\n",
    "\n",
    "# tensorboard --logdir=\"./tbX/\"\n",
    "\n",
    "# その後、http://localhost:6006\n",
    "# にアクセスします\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "以上"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
